<!DOCTYPE html>
<html lang="zh_cn">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>添加发货单</title>
	<link rel="stylesheet" href="./css/normalize.css">
	<link rel="stylesheet" href="./css/style.css">
</head>
<body>
	<div id="loading"></div>

	<div id="wrap">
		<div class="detail basic">
			<div class="cont">
				<p><span>物流单号:</span><input type="text" name="dCode" id="dCode"></p>
				<p><span>备注:</span><textarea name="dMemo" id="dMemo"></textarea></p>
			</div>
		</div>

		<div class="detail order">
			<div class="title nomore">选择订单</div>
			<div class="cont">
				<div class="search">
					<input type="text" name="" id="searchKey" class="inputSearch">
					<button class="btnSearch">搜索</button>
				</div>
				<div class="orderList"></div>
			</div>
		</div>
	</div>

	<div id="footer">
		<div class="btnArea clearfix">
			<button type="button" class="agree">提交</button>
			<button type="button" class="close">取消</button>
		</div>
	</div>

	<script src="./js/jquery-1.12.4.min.js"></script>
	<script src="./js/common.js"></script>
	<script src="./js/custom.js"></script>
	<script>
		var loadSkip = 0;
		var loadFlag = true;
		
		function getLoad(skip){
			loadFlag = false;
			var postData = {
				OrderStatus: '未完成',
				Sturt: 2,
				IsSupplier: true,
				Filter: $('#searchKey').val(),
				MaxResultCount: 10,
				SkipCount: loadSkip,
			};

			loadShow();
			$.ajax({
				url:"https://mom.51xietong.cn/appsms_api/api/services/app/OutsourcingWorkflow/GetAllProcessingOrderDto",
				type: "GET",
				headers: {
					Authorization: 'Bearer '+localStorage.getItem('accessToken')
				},
				dataType: "json",
				data: postData,
				complete: function (){
					loadHide();
				},
				success: function (data) {
					if(data && data.result && data.result instanceof Object){
						var item = data.result;
						var orderList = $('#wrap .detail.order .cont .orderList');
						var aUl;
						aUl = orderList.children('ul').length > 0 ? orderList.children('ul') : $('<ul></ul>').appendTo(orderList);

						for ( i in item.items ) {
							var ival = item.items[i];
							var aLi = $('<li></li>').appendTo(aUl);
							var aOrder = $('<div></div>').addClass('orderInfo').appendTo(aLi);
							aOrder.append($('<input>').attr('type','radio').attr('name','order').attr('id','order_'+fmt(ival.id)).data('ival',ival));
							var aLabel = $('<label></label>').attr('for','order_'+fmt(ival.id)).appendTo(aOrder);
							aLabel.append($('<p></p>').text(fmt(ival.orderCode)).prepend($('<span></span>').text('订单号:')));
							aLabel.append($('<p></p>').text(fmt(ival.creationTime,'date')).prepend($('<span></span>').text('发起日期:')));
						}

						loadSkip += 10;
						if ( loadSkip < item.totalCount ) {
							loadFlag = true;
						}
					}
				}
			});
		}

		$(window).on("scroll", function () {
			if ( $(document).scrollTop() + $(window).height() > $(document).height() - 20 && loadFlag) {
				getLoad(loadSkip);
			}
		});

		$('#wrap').on('keyup','.detail.order .search .inputSearch',function(e){
			if ( e.which == 13 ) {
				$('#wrap .detail.order .search .btnSearch').click();
			}
		});

		$('#wrap').on('click','.detail.order .cont .search .btnSearch',function(){
			loadSkip = 0;
			$('#wrap .detail.order .cont .orderList').text('');
			getLoad(loadSkip);
		});

		$('#wrap').on('change','.detail.order .orderList .orderInfo input[name=order]',function(){
			var ival = $(this).data('ival');
			var aLi = $(this).parent().parent();
			var devInfo = aLi.children('.devInfo').length > 0 ? aLi.children('.devInfo') : $('<div></div>').addClass('devInfo').appendTo(aLi);
			devInfo.text('');
			for ( j in ival.processings ) {
				var jval = ival.processings[j];
				var aReceive = $('<div></div>').addClass('receive').addClass('clearfix').appendTo(devInfo);

				aReceive.append($('<p></p>').addClass('w100').text(fmt(jval.taskName)).prepend($('<span></span>').text('产品名称:')));
				aReceive.append($('<p></p>').text(fmt(jval.taskCode)).prepend($('<span></span>').text('申请单编号:')));
				aReceive.append($('<p></p>').text(fmt(jval.type)).prepend($('<span></span>').text('产品类型:')));
				aReceive.append($('<p></p>').text(fmt(jval.modelCode)).prepend($('<span></span>').text('规格:')));
				aReceive.append($('<p></p>').text(fmt(jval.unitPrice)).prepend($('<span></span>').text('采购单价:')));
				aReceive.append($('<p></p>').text(fmt(jval.processingAmount)).prepend($('<span></span>').text('采购数量:')));
				aReceive.append($('<p></p>').text(fmt(jval.totalPrice)).prepend($('<span></span>').text('采购总价:')));
				aReceive.append($('<p></p>').text(fmt(jval.progRessrequest,'date')).prepend($('<span></span>').text('交货日期:')));
				
				var iReceive = 0;
				loadShow();
				$.ajax({
					async: false,
					url:"https://mom.51xietong.cn/appsms_api/api/services/app/DeliveryMain/GetAllAlreadyReceivingAmount",
					type: "GET",
					headers: {
						Authorization: 'Bearer '+localStorage.getItem('accessToken')
					},
					data: {'id': jval.id},
					success: function (data) {
						if(data && data.result){
							iReceive = fmt(data.result);
						}
					}
				});
				loadHide();
				jval.alreadyReceivingAmount = iReceive;
				aReceive.data('jval',jval);
				
				aReceive.append($('<p></p>').addClass('special').text(fmt(jval.processingAmount)+'/'+iReceive).prepend($('<span></span>').text('采购/已收货:  ')));
				aReceive.append($('<p></p>').prepend($('<span></span>').text('发货日期:')).append($('<input>').attr('type','date').attr('name','rDate_'+fmt(jval.id)).attr('id','rDate_'+fmt(jval.id))));
				aReceive.append($('<p></p>').prepend($('<span></span>').text('发货数量:')).append($('<input>').attr('type','number').attr('name','rCount_'+fmt(jval.id)).attr('id','rCount_'+fmt(jval.id))));
				aReceive.append($('<button></button>').attr('type','button').addClass('del'));
			}

			aLi.children('.orderInfo').addClass('on');
			devInfo.slideDown('fast');
			var oLi = aLi.siblings();
			for ( i = 0; i < oLi.length; i++ ){
				ival = oLi.eq(i);
				ival.children('.orderInfo').removeClass('on');
				ival.children('.devInfo').slideUp('fast');
			}
		});

		$('#wrap').on('click','.detail.order .orderList .devInfo .receive .del',function(){
			var _info = $(this).parent().parent();
			if ( _info.children('.receive').length <= 1 ){
				// alert('"交货单" 不能少于一个！');
				confirm('"交货单" 不能少于一个！','确定',function(){});
			} else {
				$(this).parent().remove();
			}
		});

		$('#footer').on('click','.btnArea button.agree',function(){
			var _radio = $('#wrap .detail.order .orderList .orderInfo input[name=order]:checked');

			if ( $('#dCode').val() == '' ){
				// alert('未填写 "物流单号"!');
				confirm('未填写 "物流单号"!','确定',function(){
					$('#dCode').focus();
				});
				
			} else if ( _radio.length == 0 ){
				// alert('未选择 "订单"!');
				confirm('未选择 "订单"!','确定',function(){});
			} else {
				var addFlag = true;
				var _input = _radio.parent().parent().find('input');

				for ( i=0; i<_input.length;i++ ){
					var ival = _input.eq(i);
					if ( ival.val() == '' ){
						var prdName = ival.parent().parent().data('jval').taskName;
						// alert('未填写 "'+prdName+'" 的 "'+ival.siblings('span').text()+'"!');
						confirm('未填写 "'+prdName+'" 的 "'+ival.siblings('span').text()+'"!','确定',function(){
							ival.focus();
						});
						addFlag = false;
						break;
					}
				}

				if (addFlag){
					var ival = _radio.data('ival');
					var postData = {
						"supplierUnit": ival.supplierUnit,
						"orderCode": ival.orderCode,
						"logisticsCode": $('#dCode').val(),
						"remarks": $('#dMemo').val(),
						"processingOrderId": ival.id,
						"deliveries":[]
					};
					var _receive = _radio.parent().parent().find('.receive');
					for ( j=0;j<_receive.length;j++ ){
						jval = _receive.eq(j).data('jval');
						postData.deliveries.push({
							'id': jval.id,
							'taskCode': jval.taskCode,
							'picNo': jval.picNo,
							'processingId': jval.id,
							'unit': jval.unit,
							'type': jval.type,
							'taskForModelId': jval.taskForModelId,
							'taskName': jval.taskName,
							'modelCode': jval.modelCode,
							'deliveryAmount': $('#rCount_'+jval.id).val(),
							'deliveryDate': fmt($('#rDate_'+jval.id).val(),'pdate'),
							'processingAmount': jval.processingAmount,
							'progRessrequest': jval.progRessrequest,
							'receivingStatus': -1,
							'alreadyReceivingAmount': jval.alreadyReceivingAmount
						});
					}

					loadShow();
					$.ajax({
						url: "https://mom.51xietong.cn/appsms_api/api/services/app/DeliveryMain/AddDeliveryMain",
						type: "POST",
						contentType: "application/json",
						headers: {
							Authorization: 'Bearer '+localStorage.getItem('accessToken')
						},
						dataType: "json",
						data: JSON.stringify(postData),
						complete: function (){
							loadHide();
						},
						success: function(data){
							// alert('当前请求已成功处理！');
							confirm('当前请求已成功处理！','确定',function(){
								window.location.href = "./notReceive.html";
							});
						},
						error: function (){
							// alert('抱歉！当前请求处理失败！');
							confirm('抱歉！当前请求处理失败！','确定',function(){});
						}
					});
				}
			}
		})

		$(function(){
			//
		});
	</script>
</body>
</html>